package com.bckj.fastboot.core.extra.relational;

import com.bckj.fastboot.core.lang.model.PageResult;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.simple.JdbcClient;

import java.util.List;

/**
 * AbstractRelationalRepository
 *
 * @author wfc
 **/
public class AbstractRelationalRepository<Entity, ID> implements IRelationalRepository<Entity, ID> {

    private final RelationalOps relationalOps;
    private final Class<Entity> entityClass;

    public AbstractRelationalRepository(
            RelationalOps relationalOps,
            Class<Entity> entityClass) {
        this.relationalOps = relationalOps;
        this.entityClass = entityClass;
    }

    @Override
    public boolean save(Entity entity) {
        return relationalOps.save(entity) > 0;
    }

    @Override
    public boolean saveBatch(Iterable<Entity> entities) {
        relationalOps.saveBatch(entities);
        return true;
    }

    @Override
    public boolean updateById(Entity entity) {
        return relationalOps.updateById(entity) > 0;
    }

    @Override
    public boolean updateBatchById(Iterable<Entity> entities) {
        relationalOps.updateBatchById(entities);
        return true;
    }

    @Override
    public int update(Query query, Update update) {
        return relationalOps.update(query, update);
    }

    @Override
    public boolean removeById(ID id) {
        return relationalOps.removeById(id, entityClass) > 0;
    }

    @Override
    public boolean remove(Query query) {
        return relationalOps.remove(query) > 0;
    }

    @Override
    public Entity findById(ID id) {
        return relationalOps.findById(id, entityClass);
    }

    @Override
    public <T> T findById(ID id, Class<T> entityClass) {
        return relationalOps.findById(id, entityClass);
    }

    @Override
    public List<Entity> find(Query query) {
        return relationalOps.find(query, entityClass);
    }

    @Override
    public <T> List<T> find(Query query, Class<T> entityClass) {
        return relationalOps.find(query, entityClass);
    }

    @Override
    public long count(Query query) {
        return relationalOps.count(query);
    }

    @Override
    public PageResult<Entity> page(Pageable pageable, Query query) {
        return relationalOps.page(pageable, query, entityClass);
    }

    @Override
    public PageResult<Entity> page(Pageable pageable, Query query, Class<Entity> entityClass) {
        return relationalOps.page(pageable, query, entityClass);
    }

    @Override
    public JdbcClient.StatementSpec sql(String sql) {
        return relationalOps.sql(sql);
    }

    @Override
    public JdbcClient.StatementSpec execute(Query query) {
        return relationalOps.execute(query);
    }

    @Override
    public JdbcClient.StatementSpec execute(Query query, Update update) {
        return relationalOps.execute(query, update);
    }

}
